﻿
@using IOA.MES.DataAccess.Enum
@{
    ViewBag.Title = "Oba管理";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}
@section Contents{
    <style>
        .layui-form-label {
            width: auto;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.OrderNo">订单号：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="orderNo" placeholder="请输入订单号" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.OrderNo">Oba单号：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="obaNo" placeholder="请输入Oba单号" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.CreateDate">创建日期：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtRangeDate" placeholder="请选择创建日期" value="@(DateTime.Today.AddDays(-7).ToString("yyyy-MM-dd")+" 至 "+DateTime.Today.AddDays(1).ToString("yyyy-MM-dd"))" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.InspectStatus">检验状态：</label>
                        <div class="layui-input-inline">
                            <select lay-filter="selStatus" name="status">
                                <option value="">所有状态</option>
                                @foreach (var item in ViewBag.ObaStatusEnums as List<KeyValuePair<int, string>>)
                                {
                                    <option value="@item.Key">@item.Value</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="submit" lay-submit lay-filter="submit">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        setTimeout(function () {
            initLayui();
        }, 50);

        function initLayui() {
            layui.use(['table', 'layer', 'form','laydate'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form,
                    laydate = layui.laydate;
                laydate.render({
                    elem: '#txtRangeDate'
                    , range: '至'
                });
                var options = {
                    elem: '#table'
                    , url: '/Quality/QueryObas'
                    , method: "post"
                    , where: { orderNo: "", obaNo: "", startDate: "@DateTime.Today.AddDays(-7).ToString("yyyy-MM-dd")", endDate: "@DateTime.Today.AddDays(1).ToString("yyyy-MM-dd")", status: null }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers", width: 60, fixed: 'left' }
                        , { field: 'ObaNo', title: 'Oba单号', minWidth: 150, fixed: 'left' }
                        , { field: 'OrderNo', title: '订单号', minWidth: 150, fixed: 'left' }
                        , { field: 'CustomerCode', title: '客户代码', minWidth: 100 }
                        , { field: 'CustomerName', title: '客户名称', minWidth: 150 }
                        , {
                            title: '检验项目', minWidth: 200, templet: function (d) {
                                var sites = new Array();
                                for (var i = 0; i < d.Sites.length; i++) {
                                    sites.push(d.Sites[i].CNSiteType);
                                }
                                return sites.join(", ");
                            }
                        }
                        , {
                            title: '检验包装', minWidth: 200, templet: function (d) {
                                var packs = new Array();
                                for (var i = 0; i < d.Packs.length; i++) {
                                    packs.push(d.Packs[i].PackNo);
                                }
                                return packs.join(", ");
                            }
                        }
                        , { field: 'CNCreateBy', title: '创建人', minWidth: 80 }
                        , { field: 'CreateTimeStr', title: '创建时间', minWidth: 150 }
                        , { field: 'FinishTimeStr', title: '完成时间', minWidth: 150 }
                        , {
                            title: '检验状态', minWidth: 100, fixed: 'right', templet: function (d) {
                                return "<span style='color:" + (d.Status == 0 ? "#FF5722" : "#009688") + ";'>" + d.CNStatus + "</span>";
                            }
                        }
                        , {
                            title: '操作', width: 220, fixed: 'right', templet: function (d) {
                                var str = '<button class="layui-btn layui-btn-sm layui-btn-warn" onclick="obaList(\'' + d.ObaNo + '\')"><i class="layui-icon">&#xe656;</i>检测清单</button>\
                                           <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="finishOba(\'' + d.ObaNo + '\')"><i class="layui-icon">&#xe605;</i>完结检验</button>';
                                return str;
                            }
                        }
                    ]]
                };

                function queryTable(data) {
                    options.page.curr = 1;
                    options.where = data;

                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }

                form.on('submit(submit)', function (data) {
                    console.log(data.field);
                    var forms = data.field;
                    var rangeTime = $("#txtRangeDate").val().trim() == '' ? new Array() : $("#txtRangeDate").val().trim().split(' 至 ');
                    forms.startDate = rangeTime.length <= 0 ? null : rangeTime[0];
                    forms.endDate = rangeTime.length <= 0 ? null : rangeTime[1];
                    forms.status = forms.status == "" ? null : parseInt(forms.status);
                    queryTable(forms);
                    return false;
                });
                $("#btnSearch").click();
            });
        }

        function obaList(obaNo) {
            layer.open({
                type: 2,
                shade: [0.1, '#000'],
                area: ['850px', '600px'],
                content: "/Quality/ObaList?obaNo=" + obaNo
            });
        }

        function finishOba(obaNo) {
            var loadId = layer.load();
            $.ajax({
                url: "/Quality/FinishOba",
                data: {
                    obaNo: obaNo
                },
                complete: function () {
                    layer.close(loadId);
                },
                success: function (res) {
                    if (res.ErrCode != 200) {
                        layer.msg(res.ErrMsg, { icon: 5 });
                        return;
                    }
                    layer.msg("完结成功！", { icon: 6, time: 1000 });
                    $("#btnSearch").click();
                }
            });
        }
    </script>
}
